//
//  playerAddressViewController.m
//  SoundPill
//
//  Created by 宋旭东 on 16/8/31.
//  Copyright © 2016年 宋旭东. All rights reserved.
//
#define NavigationBar_HEIGHT 44
#define labelGrayColor [UIColor colorWithRed:0.961 green:0.965 blue:0.969 alpha:1.00]
#import "playerAddressViewController.h"
#import "UIViewExt.h"
#import "JCAlertView.h"
@interface playerAddressViewController ()<UITextFieldDelegate,UITextViewDelegate>
@property (strong, nonatomic) UIScrollView *scorllView;
@property (strong, nonatomic) UITextField *nameField;
@property (strong, nonatomic) UITextField *phoneField;
@property (strong, nonatomic) UITextField *provincesField;
@property (strong, nonatomic) UITextField *cityField;
@property (strong, nonatomic) UITextField *districtField;
@property (strong, nonatomic) UITextView *addressDetailView;
@property (strong, nonatomic) UITextView *noteTextView;
@end

@implementation playerAddressViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = labelGrayColor;
    self.title = @"收件人信息";
    // Do any additional setup after loading the view.
    UIButton *back = [UIButton buttonWithType:UIButtonTypeCustom];
    [back setFrame:CGRectMake(0, 0, 30, 30)];
    [back setImage:[UIImage imageNamed:@"backButton"] forState:UIControlStateNormal];
    [back addTarget:self action:@selector(backButtonClick:) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem *backItem = [[UIBarButtonItem alloc] initWithCustomView:back];
    self.navigationItem.leftBarButtonItem = backItem;

    UIButton *releaseButton = [UIButton buttonWithType:UIButtonTypeCustom];
    releaseButton.frame = CGRectMake(0, 0, 40, 30);
    [releaseButton setTitle:@"完成" forState:normal];
    [releaseButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [releaseButton addTarget:self action:@selector(doneButtonClick:) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem *releaseButtonItem = [[UIBarButtonItem alloc] initWithCustomView:releaseButton];
    self.navigationItem.rightBarButtonItem = releaseButtonItem;
    [self createSubviews];
}
- (void)createSubviews
{
    
    self.scorllView = [[UIScrollView alloc] initWithFrame:self.view.bounds];
    _scorllView.backgroundColor = labelGrayColor;
    [self.view addSubview:_scorllView];
    
    UILabel *contact = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, self.view.width, NavigationBar_HEIGHT)];
    contact.backgroundColor = [UIColor clearColor];
    contact.text = @"    联系人信息";
    [self.scorllView addSubview:contact];
//
    UIView *leftView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 20, NavigationBar_HEIGHT)];
    self.nameField = [[UITextField alloc] initWithFrame:CGRectMake(0, contact.bottom, self.view.width, NavigationBar_HEIGHT)];
    _nameField.backgroundColor = [UIColor whiteColor];
    _nameField.leftViewMode = UITextFieldViewModeAlways;
    _nameField.leftView = leftView;
    _nameField.returnKeyType = UIReturnKeyNext;
    _nameField.placeholder = @"收货人姓名";
    _nameField.delegate = self;
    [_nameField setValue:[UIColor colorWithRed:0.435 green:0.435 blue:0.435 alpha:1.00] forKeyPath:@"_placeholderLabel.textColor"];
    [self.scorllView addSubview:_nameField];
//
    UIView *phoneleftView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 20, NavigationBar_HEIGHT)];
    self.phoneField = [[UITextField alloc] initWithFrame:CGRectMake(0, _nameField.bottom + 2, self.view.width, NavigationBar_HEIGHT)];
    _phoneField.backgroundColor = [UIColor whiteColor];
    _phoneField.placeholder = @"联系电话";
    _phoneField.delegate = self;
    _phoneField.returnKeyType = UIReturnKeyNext;
    _phoneField.keyboardType = UIKeyboardTypePhonePad;
    _phoneField.leftViewMode = UITextFieldViewModeAlways;
    _phoneField.leftView = phoneleftView;
    [_phoneField setValue:[UIColor colorWithRed:0.435 green:0.435 blue:0.435 alpha:1.00] forKeyPath:@"_placeholderLabel.textColor"];
    [self.scorllView addSubview:_phoneField];
//
    UILabel *addressLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, _phoneField.bottom, self.view.width, NavigationBar_HEIGHT)];
    addressLabel.text = @"    收货地址";
    addressLabel.backgroundColor = [UIColor clearColor];
    [self.scorllView addSubview:addressLabel];
//
    UIView *provincesleftView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 20, NavigationBar_HEIGHT)];
    self.provincesField = [[UITextField alloc] initWithFrame:CGRectMake(0, addressLabel.bottom, self.view.width, NavigationBar_HEIGHT)];
    _provincesField.backgroundColor = [UIColor whiteColor];
    _provincesField.placeholder = @"省";
    _provincesField.delegate = self;
    _provincesField.returnKeyType = UIReturnKeyNext;
    _provincesField.leftViewMode = UITextFieldViewModeAlways;
    _provincesField.leftView = provincesleftView;
    [_provincesField setValue:[UIColor colorWithRed:0.435 green:0.435 blue:0.435 alpha:1.00] forKeyPath:@"_placeholderLabel.textColor"];
    [self.scorllView addSubview:_provincesField];
//
     UIView *cityleftView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 20, NavigationBar_HEIGHT)];
    self.cityField = [[UITextField alloc] initWithFrame:CGRectMake(0, _provincesField.bottom + 2, self.view.width, NavigationBar_HEIGHT)];
    _cityField.backgroundColor = [UIColor whiteColor];
    _cityField.placeholder = @"市";
    _cityField.delegate = self;
    _cityField.returnKeyType = UIReturnKeyNext;
    _cityField.leftViewMode = UITextFieldViewModeAlways;
    _cityField.leftView = cityleftView;
     [_cityField setValue:[UIColor colorWithRed:0.435 green:0.435 blue:0.435 alpha:1.00] forKeyPath:@"_placeholderLabel.textColor"];
    [self.scorllView addSubview:_cityField];
//
     UIView *districtleftView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 20, NavigationBar_HEIGHT)];
    self.districtField = [[UITextField alloc] initWithFrame:CGRectMake(0, _cityField.bottom + 2, self.view.width, NavigationBar_HEIGHT)];
    _districtField.backgroundColor = [UIColor whiteColor];
    _districtField.placeholder = @"区";
    _districtField.delegate = self;
    _districtField.returnKeyType = UIReturnKeyNext;
    _districtField.leftViewMode = UITextFieldViewModeAlways;
    _districtField.leftView = districtleftView;
    [_districtField setValue:[UIColor colorWithRed:0.435 green:0.435 blue:0.435 alpha:1.00] forKeyPath:@"_placeholderLabel.textColor"];
    [self.scorllView addSubview:_districtField];
//
//     UIView *detailleftView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 20, NavigationBar_HEIGHT)];
    self.addressDetailView = [[UITextView alloc] initWithFrame:CGRectMake(0, _districtField.bottom + 2, self.view.width, 2 * NavigationBar_HEIGHT)];
    _addressDetailView.backgroundColor = [UIColor whiteColor];
    _addressDetailView.font = _districtField.font;
    _addressDetailView.text = @"详细地址";
    _addressDetailView.textContainerInset = UIEdgeInsetsMake(15, 15, 0, 0);
    _addressDetailView.textColor = [UIColor colorWithRed:0.435 green:0.435 blue:0.435 alpha:1.00];
    _addressDetailView.delegate = self;
    [self.scorllView addSubview:_addressDetailView];
    
    UILabel *expressWay = [[UILabel alloc] initWithFrame:CGRectMake(0, _addressDetailView.bottom + 5, self.view.width, NavigationBar_HEIGHT)];
    expressWay.backgroundColor = [UIColor whiteColor];
    expressWay.textColor = [UIColor colorWithRed:0.796 green:0.796 blue:0.796 alpha:1.00];
    expressWay.text = @"    运输方式：顺丰付款";
    [self.scorllView addSubview:expressWay];
    
    self.noteTextView = [[UITextView alloc] initWithFrame:CGRectMake(0, expressWay.bottom + 5, expressWay.width, expressWay.height * 2)];
    _noteTextView.text = @"备注";
    _noteTextView.backgroundColor = [UIColor whiteColor];
    _noteTextView.font = _districtField.font;
    _noteTextView.textContainerInset = UIEdgeInsetsMake(15, 15, 0, 0);
    _noteTextView.textColor = [UIColor colorWithRed:0.435 green:0.435 blue:0.435 alpha:1.00];
    _noteTextView.delegate = self;
    [self.scorllView addSubview:_noteTextView];
    
    if ([[NSUserDefaults standardUserDefaults] objectForKey:@"userAddress"] != nil || [[NSUserDefaults standardUserDefaults] objectForKey:@"userAddress"] != NULL) {
        NSDictionary *dictionary = [[NSUserDefaults standardUserDefaults] objectForKey:@"userAddress"];
        _nameField.text = [dictionary valueForKey:@"name"];
        _phoneField.text = [dictionary valueForKey:@"phone"];
        _provincesField.text = [dictionary valueForKey:@"provinces"];
        _cityField.text = [dictionary valueForKey:@"city"];
        _districtField.text = [dictionary valueForKey:@"district"];
        _addressDetailView.text = [dictionary valueForKey:@"detail"];
        _addressDetailView.textColor = [UIColor blackColor];
    }
}
- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
    if (textField == _nameField) {
        [_phoneField becomeFirstResponder];
    } else if (textField == _phoneField) {
        [_provincesField becomeFirstResponder];
    } else if (textField == _provincesField) {
        [_cityField becomeFirstResponder];
    } else if (textField == _cityField) {
        [_districtField becomeFirstResponder];
    } else if (textField == _districtField) {
        [_districtField resignFirstResponder];
//        [_addressDetailView becomeFirstResponder];
    }
    return YES;
}
- (BOOL)textViewShouldBeginEditing:(UITextView *)textView
{
    if (textView == _addressDetailView) {
        [_scorllView setContentOffset:CGPointMake(0, _provincesField.top) animated:YES];
        if ([_addressDetailView.text isEqualToString:@"详细地址"]) {
            _addressDetailView.text = nil;
        } else {
        }
        _addressDetailView.textColor = [UIColor blackColor];
    } else {
        [_scorllView setContentOffset:CGPointMake(0, _districtField.top) animated:YES];
        if ([textView.text isEqualToString:@"备注"]) {
            textView.text = nil;
        }
    }
    return YES;
}
- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text
{
    if ([text isEqualToString:@"\n"]) {
        [_scorllView setContentOffset:CGPointMake(0, 0) animated:YES];
        [textView resignFirstResponder];
        if (textView == _addressDetailView) {
//            if ([textView.text stringByReplacingOccurrencesOfString:@" " withString:@""].length == 0) {
////                textView.text = @"详细地址";
//            }
        } else {
            if ([textView.text stringByReplacingOccurrencesOfString:@" " withString:@""].length == 0) {
                textView.text = @"备注";
            }
        }
    } else {
        
    }
    return YES;
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
- (void)backButtonClick:(UIButton *)sender
{
    [self.navigationController popViewControllerAnimated:YES];
}
- (void)doneButtonClick:(UIButton *)sender
{
    if (_nameField.text.length > 0 && _phoneField.text.length > 0 && _provincesField.text.length > 0 && _cityField.text.length > 0 && _districtField.text.length > 0 && _addressDetailView.text.length > 0 && ![_addressDetailView.text isEqualToString:@"详细地址"]) {
        NSDictionary *addressDic = [NSDictionary dictionaryWithObjectsAndKeys:_nameField.text,@"name",_phoneField.text,@"phone",_provincesField.text,@"provinces",_cityField.text,@"city",_districtField.text,@"district",_addressDetailView.text,@"detail", nil];
        [[NSUserDefaults standardUserDefaults] setObject:addressDic  forKey:@"userAddress"];
        if (_noteTextView.text.length > 0 && ![_noteTextView.text isEqualToString:@"备注"]) {
            [[NSUserDefaults standardUserDefaults] setValue:_noteTextView.text forKey:@"payNote"];
        }
        [self backButtonClick:nil];
    } else {
       [JCAlertView showOneButtonWithTitle:@"提示" Message:@"请补全收件人信息" ButtonType:JCAlertViewButtonTypeDefault ButtonTitle:@"确定" Click:^{
           
       }];
    }
}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
